#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#include <iostream>
using namespace std;

// Checks whether a user enters a valid username and a valid password
bool credentialCheck(char uname[], char pass[]) {
    if(strcmp(uname, "sBob") == 0 && strcmp(pass, "iLikeYellow") == 0){
        return true;
    }
    return false;
}

int main(int argc, char**argv) {
    int loggedIn = 0;
    int attempts = 0;

    printf("RealBank eBanking\n");
    printf("A checking account that fits your schedule\n\n");
    
    // Checks whether the user is logged in
    // Locks the user's account if the user fails to login at his/her third attempts
    while(!loggedIn) {
        enum { MAXSIZE = 15, MAX_LOGIN_ATTEMPT = 3};
        int index=0;
        int i = 0;
        char cUsername[MAXSIZE];
        char cPassword[MAXSIZE] = {0};
        
        attempts++;
        if(attempts > MAX_LOGIN_ATTEMPT) {
            printf("Unsuccessful login! Your account has been locked!\n"); 
            printf("Please contact your Bank representative!\n");
            getchar();
            break;
        }
        
        // Asks user to enter his/her username
        puts("\nUsername: ");
        // Handles maximum number of characters to be read (including the final null-character)
        fgets(cUsername,MAXSIZE,stdin);
        cUsername[(int)strlen(cUsername)-1] = 0;    // add a null terminating character
        
        // Asks user to enter his/her password
        // Displays asterisk characters instead of plain text
        puts("\nPassword : ");
        for(i = index; i < MAXSIZE; index++) {
            char c = _getch();
            if(c == '\r') {     // Watch for newline (CR)
                break;
            }
            // Bound checking for the number of characters entered
            if(index >= MAXSIZE) {
                printf("\nYour password should not be longer than 10 characters!\n");
                printf("Please re-enter your password: ");
                i = 0;
                index = 0;
                cPassword[MAXSIZE];
                continue;
            }
            cPassword[index] = c;
            cout << "*";
        }
        cPassword[index+1] = 0;  // add a null terminating character
        
        // Checks the validity of user's credential
        if(credentialCheck(cUsername, cPassword)) {
            loggedIn = 1;
            system("cls");
            printf("RealBank                                    Online Banking\n\n");
            printf("Welcome %s \n", cUsername);
            printf("----------------------------------------------------------\n");
            printf("Accounts Overview\n");
            printf("Transfer\n");
            printf("Interest\n");
            printf("Free Bill Payer\n");
            printf("Text Banking\n");
            printf("Customer Service\n");
            getchar();
        }
        else {
            system("cls");
            printf("Invalid username or/and password.\n");
            printf("Please re-enter the your username and password.\n");
            printf("-----------------------------------------------\n");
        }
    }
    return 0;
}